package com.capt_4.ui;

import com.capt_4.Answer;
import com.capt_4.Check;
import com.capt_4.Exercise;
import com.capt_4.io.EquationDao;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class SystemUi extends JFrame {

    private JPanel contentPanel = null;
    private JPanel panelCenter;
    private JPanel panelSouth;
    private JTextField[] tAnswer,tEquation;
    private JButton prePage;
    private JButton pageIndex;
    private JButton nextPage;
    private JButton check;
    private JLabel resultLable;

    private int count,currentPage,pageCount,pageSize;
    private Exercise exercise;
    private Answer answer;

    private static SystemUi systemUi;

    public static void main(String[] args) {
        systemUi = new SystemUi();


    }
    // 界面的基本设置
    public SystemUi(){
        setTitle("算式练习题");
        // 用来设置窗口的坐标和尺寸
//        setBounds(400,200,1000,500);
        setBounds(500,200,500,400);
        // 设置窗口可见
        setVisible(true);
//        点击关闭按钮，结束程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建面板
        contentPanel = new JPanel();
        // 设置面板外
        contentPanel.setBorder(new EmptyBorder(5,5,5,5));

        // 面板布局:边框布局
        contentPanel.setLayout(new BorderLayout());
        // 设置窗体不能调整大小
//        setResizable(false);
        // 将面板对象设置为窗体主面板
        setContentPane(contentPanel);

        // 详细的菜单设置
        init();
    }



    private void init() {
        //  创建菜单栏
        JMenuBar menuBar = new JMenuBar();
        // 将菜单栏添加到窗体中
        setJMenuBar(menuBar);

        // 创建菜单按钮:在线练习
        JMenu menuOnline = new JMenu("在线练习");
        // 将菜单添加到菜单栏
        menuBar.add(menuOnline);
        // 创建菜单项
        JMenuItem addOnline = new JMenuItem("加法练习");
        // 添加警探对象
        addOnline.setActionCommand("addOnline");
        addOnline.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onLine(e);
            }
        });
        JMenuItem subOnline = new JMenuItem("减法练习");
        subOnline.setActionCommand("subOnline");
        subOnline.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onLine(e);
            }
        });
        JMenuItem mixOnline = new JMenuItem("混合练习");
        mixOnline.setActionCommand("mixOnline");
        mixOnline.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onLine(e);
            }
        });

        // 将菜单项添加到菜单栏目中
        menuOnline.add(addOnline);
        menuOnline.add(subOnline);
        menuOnline.add(mixOnline);


        // 生成习题
        JMenu menuOutline = new JMenu("生成练习");
        // 将菜单添加到菜单栏
        menuBar.add(menuOutline);
        // 创建菜单项
        JMenuItem addOutline = new JMenuItem("加法习题");
        // 添加警探对象
        addOutline.setActionCommand("addOutline");
        addOutline.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                outLine(e);
            }
        });
        JMenuItem subOutline = new JMenuItem("减法习题");
        subOutline.setActionCommand("subOutline");
        subOutline.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                outLine(e);
            }
        });
        JMenuItem mixOutline = new JMenuItem("混合习题");
        mixOutline.setActionCommand("mixOutline");
        mixOutline.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                outLine(e);
            }
        });

        // 将菜单项添加到菜单栏目中
        menuOutline.add(addOutline);
        menuOutline.add(subOutline);
        menuOutline.add(mixOutline);


        // 导入习题
        JMenu readExercise = new JMenu("导入习题");
        // 将菜单添加到菜单栏
        menuBar.add(readExercise);
        // 创建菜单项
        JMenuItem read = new JMenuItem("导入...");

        read.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                // 创建文件选择器
                JFileChooser jFileChooser = new JFileChooser();
                // 设置文件选择器的默认打开位置
                jFileChooser.setCurrentDirectory(new File("/home/gonglidong/Desktop/linuxText/Maven/SpringBootMaven/CXSJ_Student/src/com/capt_4/io/"));
                // 设置文件过滤器
                // 显示文件选择器的对话框
                int i = jFileChooser.showOpenDialog(systemUi);
                String path1 = null;
                if (i == JFileChooser.APPROVE_OPTION){
                    File file = jFileChooser.getSelectedFile();
                    // 获取文件路径
                    path1 = file.getAbsolutePath();
                }
                exercise = EquationDao.readExerciseFromFile(path1);

                currentPage = 1;

                for (int j = 0; j < 50; j++) {
                    answer.set(j,-1);
                }
                update();
            }
        });

        // 将菜单项添加到菜单栏目中
        readExercise.add(read);


        // 退出
        JMenu exitSystem = new JMenu("退出系统");
        // 将菜单添加到菜单栏
        menuBar.add(exitSystem);
        // 创建菜单项
        JMenuItem exit = new JMenuItem("退出");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.exit(0);
            }
        });

        // 将菜单项添加到菜单栏目中
        exitSystem.add(exit);


        // 创建面板
        panelCenter = new JPanel();
        panelSouth = new JPanel();
        contentPanel.add(BorderLayout.CENTER,panelCenter);
        contentPanel.add(BorderLayout.SOUTH,panelSouth);

        tEquation = new JTextField[20];
        tAnswer = new JTextField[20];

        for (int i = 0; i < 20; i++) {
            tEquation[i] = new JTextField(5);
            tAnswer[i] = new JTextField(2);
            tEquation[i].setEnabled(false);
            panelCenter.add(tEquation[i]);
            panelCenter.add(tAnswer[i]);
        }


        prePage = new JButton("上一页");
        pageIndex = new JButton("第x页/共X页");
        nextPage = new JButton("下一页");

        prePage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                prePage();
            }
        });



        // 下一页
        nextPage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                nextPage();
            }
        });

        panelCenter.add(prePage);
        panelCenter.add(pageIndex);
        panelCenter.add(nextPage);


        check = new JButton("批改");
        resultLable = new JLabel("批改结果");

        check.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                check();
            }

        });

        // 将底部的布局改为流式布局
        FlowLayout flowLayout = (FlowLayout)panelSouth.getLayout();
        // 设置为左对齐
        flowLayout.setAlignment(FlowLayout.LEFT);

        panelSouth.add(check);
        panelSouth.add(resultLable);


        count = 50;
        currentPage = 1;
        pageSize = 20;
        pageCount = (int)Math.ceil(count*1.0/pageSize);

        answer = new Answer();
        for (int i = 0; i < 50; i++) {
            answer.add(-1);
        }
    }

    // 生成习题菜单逻辑
    private void outLine(ActionEvent e) {
        String command = e.getActionCommand();

//        EquationDao equationDao = new EquationDao();
        EquationDao equationDao = new EquationDao();
//        new e
        exercise = new Exercise();
        if (command.equals("addOutline")){
            exercise.generateAddExercise();
//            equationDao.wr
        } else if (command.equals("subOutline")){
            exercise.generateSubExercise();
        } else if (command.equals("mixOutline")){
            exercise.generateRandomExercise();
        }
//        JOptionPane.showMessageDialog(systemUi,"生成成功，保存位置在D盘");
    }

    // 在线练习执行逻辑
    private void onLine(ActionEvent e) {
        String command = e.getActionCommand();

        exercise = new Exercise();
        //设置加习题集土木数量
        exercise.setExerciseNumber(50);

        if (command.equals("addOnline")){
//            System.out.println("生成加法:");
            exercise.generateAddExercise();
        } else if (command.equals("subOnline")){
            exercise.generateSubExercise();
        } else if (command.equals("mixOnline")){
            exercise.generateRandomExercise();
        }
        // 重置答案为-1
        for (int i = 0; i < 50; i++) {
            answer.set(i,-1);
        }
        update();
    }

    // 刷新页面内容
    private void update() {

        for (int i = (currentPage-1)*pageSize,j=0;i < currentPage*pageSize; i++,j++) {
            if (i < count){
                String equationString = exercise.getList().get(i).toString();
                // 将算式显示到面板
                tEquation[j].setText(equationString);

                if (answer.get(i) != -1){
                    tAnswer[j].setText(Integer.toString(answer.get(i)));
                } else {
                    tAnswer[j].setText("");
                }

                tEquation[j].setVisible(true);
                tAnswer[j].setVisible(true);
            } else {
                // 题目显示完毕，剩余文本框不显示
                tEquation[j].setVisible(false);
                tAnswer[j].setVisible(false);
            }

        }
        // 设置上一页，下一页可用性
        prePage.setEnabled(currentPage>1);
        nextPage.setEnabled(currentPage<pageCount);
        pageIndex.setText("第"+currentPage+"页/共"+pageCount+"页");
        check.setEnabled(currentPage == pageCount);
    }

    // 上一页
    private void prePage(){
        for (int i = (currentPage-1)*pageSize,j=0; i < currentPage*pageSize; i++,j++) {
            if (tAnswer[j].getText().length() > 0 && i < count){
                if (tAnswer[j].getText().matches("^[0-9]*$")){
                    answer.set(i,Integer.parseInt(tAnswer[j].getText()));
                }
            }
        }
        currentPage--;
        update();
    }

    // 下一页
    private void nextPage(){
        for (int i = (currentPage-1)*pageSize,j=0; i < currentPage*pageSize; i++,j++) {
            if (tAnswer[j].getText().length() > 0 && i < count){
                if (tAnswer[j].getText().matches("^[0-9]*$")){
                    answer.set(i,Integer.parseInt(tAnswer[j].getText()));
                }
            }
        }
        currentPage++;
        update();
    }

    // 批改
    private void check() {
        for (int i = (currentPage-1)*pageSize,j=0; i < currentPage*pageSize; i++,j++) {
            if (tAnswer[j].getText().length() > 0 && i < count){
                if (tAnswer[j].getText().matches("^[0-9]*$")){
                    answer.set(i,Integer.parseInt(tAnswer[j].getText()));
                }
            }
        }

        Check check1 = new Check();
        check1.checkExercise(answer,exercise);

        // 显示答题结果
        resultLable.setText("总题目:"+count+",正确"+check1.getRight()+",错误:"+check1.getWrong());

    }

//    public void
}
